package shuaige.jdk.stream.流基础;

import java.util.Arrays;
import java.util.List;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

/**
 * 中间流操作指的是各种中间流操作方法，比如去重、过滤、排序等
 */
public class 中间流转换_flatMapToXxx {
    /**
     * 重新转换为 IntStream 流
     */
    public static void flatMapToInt() {
        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9");
        System.out.println("--------------- IntStream ---------------");
        int[] ints = list.stream()
                .map(Integer::valueOf)
                .flatMapToInt(integer -> {
                    return IntStream.of(integer);
                })
                .peek(integer -> {
                    System.out.println(integer);
                })
                .toArray();
    }

    /**
     * 重新转换为 IntStream 流
     */
    public static void flatMapToLong() {
        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9");
        System.out.println("--------------- LongStream ---------------");
        long[] longs = list.stream()
                .map(Integer::valueOf)
                .flatMapToLong(integer -> {
                    return LongStream.of(integer);
                })
                .peek(integer -> {
                    System.out.println(integer);
                })
                .toArray();

    }

    /**
     * 重新转换为 IntStream 流
     */
    public static void flatMapToDouble() {
        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9");
        list.stream()
                .map(Integer::valueOf)
                .flatMapToDouble(integer -> {
                    return DoubleStream.of(integer);
                })
                .peek(integer -> {
                    System.out.println(integer);
                })
                .toArray();

    }

    public static void main(String[] args) {

    }

}
